<?php
// +----------------------------------------------------------------------
// | Created by Sublime: [ Conqweal ]
// +----------------------------------------------------------------------
// | Copyright (c) 2021~20223[chenjia] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.99dn.cn
// +----------------------------------------------------------------------
// | Author: Conqweal <150672834@qq.com>
// +----------------------------------------------------------------------
// | Date: 2021/12/22 0026
// +----------------------------------------------------------------------
// | Description:管理中心首页
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\BackendController;
use support\Request;
use think\facade\Cache;
use think\facade\Db;


class IndexController extends BackendController
{
    public function index(Request $request)
    {

        $menus = Db::name("role_access")->where('ac_menu_flag', 1)->order('ac_order', 'asc')->column("*", "ac_id");

        $menus = list_to_tree($menus, "ac_id", "ac_pid");
        $menus = $this->fetchAccessMenus($menus);
        $uinfo = \app\admin\model\User::find($request->cookie('admin_id'));
        return view("index/index", compact('menus', 'uinfo'));

    }

    private function fetchAccessMenus($menus)
    {
        foreach ($menus as $k => $v) {
            if (!checkAccess($v['ac_id'])) {
                unset($menus[$k]);
                continue;
            }
            if (isset($v['_child'])) {
                $v['_child'] = $this->fetchAccessMenus($v['_child']);
                $menus[$k]   = $v;
            }

        }

        return $menus;
    }

    public function view(Request $request)
    {

        $data = [
            'name'  => 'thinkphp',
            'email' => 'thinkphp@qq.com',
            'age'   => 'thinkphp@qq.com',
        ];

        /*$validate = new \app\validate\User;
        $error    = "";
        if (!$validate->check($data)) {
        $error = $validate->getError();
        }
        if ($error) {
        return json(['code' => -1, 'msg' => $error]);
        }*/

        Cache::set("abc", "xxx");

        return view('index/view', ['name' => Cache::get("abc")]);
    }

    public function json(Request $request)
    {
        return json(['code' => 0, 'msg' => 'ok']);
    }

}
